﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Threading;
using System.Collections.Specialized;
using DirectoryViewModels;
using System.Threading;
using BrainTechLLC.FileMaintenance;
using BrainTechLLC.WPF;
using DragDropLibrary;

using System.ComponentModel;
using BrainTechLLC.ThreadSafeObjects;
using System.Diagnostics;

namespace BrainTechLLC.FolderExplorer
{
    public partial class FolderExplorerControl : UserControl
    {
        private bool _searchTextChanged;
        private string _searchText;
        
        public void MarkSearchTextChanged(string searchText)
        {
            _searchText = searchText;
            _searchTextChanged = true;
        }
        
        public void DoSearch(string search)
        {
            if (search == null)
            {
                search = _searchText;
            }

            if (search.Length > 2)
            {
                var folders = RootFolder.GetAllFoldersInMemory();
                var found = from f in folders where f.FullPath.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0 select f;                

                if (found.Count() > 0)
                {
                    var notFound = from f in folders where f.FullPath.IndexOf(search, StringComparison.OrdinalIgnoreCase) < 0 select f;

                    Dictionary<FolderViewModel, int> lookup = new Dictionary<FolderViewModel, int>();
                    for (int n = 0; n < _TreeViewItems.Count; n++)
                    {
                        var item = _TreeViewItems[n];
                        lookup.Add(item.Tag as FolderViewModel, n);
                    }

                    RootFolder.GetAllFoldersInMemory().ForEach(f => f.IsSelected = false);

                    foreach (var item in notFound)
                    {
                        int idx = FindIndex(item, lookup);
                        if (idx >= 0)
                        {
                            _TreeViewItems[idx].Visibility = Visibility.Collapsed;
                        }
                    }

                    foreach (var item in found)
                    {
                        if (item.Parent != null)
                        {
                            item.Parent.IsExpanded = true;
                        }
                    }

                    Dictionary<FolderViewModel, int> resultDictionary = new Dictionary<FolderViewModel, int>();

                    foreach (var item in found)
                    {
                        GetParents(item, resultDictionary);
                    }

                    List<FolderViewModel> results = resultDictionary.Keys.ToList();
                    results.Sort(new Comparison<FolderViewModel>((f1, f2) => f1.Depth.CompareTo(f2.Depth)));

                    lookup.Clear();
                    for (int n = 0; n < _TreeViewItems.Count; n++)
                    {
                        var item = _TreeViewItems[n];
                        lookup.Add(item.Tag as FolderViewModel, n);
                    }

                    for (int n = 0; n < results.Count; n++)
                    {
                        var folder = results[n];

                        int idx = FindIndex(folder, lookup);

                        if (idx >= 0)
                        {
                            _TreeViewItems[idx].Visibility = Visibility.Visible;
                        }
                    }

                    //Dispatcher.BeginInvoke(new VoidDelegate(() =>
                    //{
                    //    found.ForEach(f => f.IsSelected = true);
                    //}));

                    results.Clear();
                }
                else
                {
                    _TreeViewItems.ForEach(tvi => tvi.Visibility = Visibility.Collapsed);
                }

                tvRoot.InvalidateVisual();
            }
            else
            {
                for (int n = 0; n < _TreeViewItems.Count; n++)
                {
                    var item = _TreeViewItems[n];
                    item.Visibility = Visibility.Visible;
                }
            }
        }
    }
}
